﻿using Core.AdditionalInterfaces;
using Core.Store;
using Infrastructure.ExtendedEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
using Utils.Extentions;

namespace Infrastructure.Managers
{
    public class StoreManager : BaseManager
    {
        public IEnumerable<Store_Category_WithProperties> GetCategoriesByParentId(string lang, int categoryId = 0)
        {
            var categories = _dbContext.Get<Store_Category>(x => x.ParentId == categoryId, "Store_CategoryMainProperty,Store_Category2Product.Store_Product.Store_ProductImage", null, 0, 0).ToList();

            return ToExtendedClassObjectList<Store_Category, Store_Category_WithProperties>(categories, lang);
        }

        public IEnumerable<Store_Product_WithProperties> GetProductsByCategoryId(string lang, int categoryId)
        {
            var products = _dbContext.Get<Store_Category2Product>(x => x.Store_Category_Id == categoryId, "Store_Product", null, 0, 0).Select(x => x.Store_Product).ToList();

            return ToExtendedClassObjectList<Store_Product, Store_Product_WithProperties>(products, lang);
        }

        public Store_Product_WithProperties GetProductById(string lang, int productId)
        {
            _dbContext.IsCreateProxy = false;

            var product = _dbContext.Get<Store_Product>(x => x.Id == productId, "Store_Category2Product,Store_ProductMainProperty.Culture,Store_PropertyValue2Product.Store_PropertyValue.Store_Property", null, 0, 0).SingleOrDefault();
            FillProductWithCustomProperies(product);
            return ToExtendedClassObject<Store_Product, Store_Product_WithProperties>(product, lang);
        }

        public int CreateNewProduct(string lang, int categoryId)
        {
            var culture = _dbContext.Get<Culture>(x => x.Name == lang).SingleOrDefault();

            var product = new Store_Product();

            product.FriendlyUrl = Guid.NewGuid().ToString();

            var productMainProperty = new Store_ProductMainProperty();
            var category2Product = new Store_Category2Product();

            productMainProperty.Culture = culture;

            category2Product.Store_Category = _dbContext.Get<Store_Category>(x => x.Id == categoryId).SingleOrDefault();

            product.Store_ProductMainProperty = new List<Store_ProductMainProperty>();
            product.Store_ProductMainProperty.Add(productMainProperty);

            product.Store_Category2Product = new List<Store_Category2Product>();
            product.Store_Category2Product.Add(category2Product);

            _dbContext.Insert<Store_Product>(product);

            _dbContext.Save();

            return product.Id;
        }

        public void FillProductWithCustomProperies(Store_Product product)
        {
            var allProperties = _dbContext.Get<Store_Property>();
            foreach (var property in allProperties)
            {
                if (product.Store_PropertyValue2Product.Where(x => x.Store_PropertyValue.Store_Property.Id == property.Id).Count() == 0)
                {
                    var store_PropertyValue2Product = new Store_PropertyValue2Product() { SortOrder = 0 };
                    var store_PropertyValue = new Store_PropertyValue() { SortOrder = 0, IsUseInFilter = false };
                    store_PropertyValue.Value = String.Empty;
                    if (property.Store_PropertyValue == null)
                    {
                        property.Store_PropertyValue = new List<Store_PropertyValue>();
                    }
                    store_PropertyValue.Store_Property = property;
                    property.Store_PropertyValue.Add(store_PropertyValue);
                    store_PropertyValue2Product.Store_PropertyValue = store_PropertyValue;
                    product.Store_PropertyValue2Product.Add(store_PropertyValue2Product);
                    
                }                               
            }

            _dbContext.Save();
        }

        public string DeleteProduct(int id)
        {
            try
            {
                _dbContext.Delete<Store_Product>(id);
                _dbContext.Save();
                return "OK";
            }
            catch (Exception e)
            {
                return e.BuildErrorString();
            }
        }

        //need to improve
        public string SaveCategory(Store_Category_WithProperties category, string lang)
        {

            if (category.Id != 0)
            {
                var existedCategory = _dbContext.Get<Store_Category>(x => x.Id == category.Id).SingleOrDefault();//new Store_Category_WithProperties(_dbContext.Get<Store_Category>(x => x.Id == category.Id).SingleOrDefault(), lang);

                existedCategory.FullImgLink = category.FullImgLink;
                existedCategory.SmallImgLink = category.SmallImgLink;
                existedCategory.FriendlyUrl = category.FriendlyUrl;

                existedCategory.Store_CategoryMainProperty.SingleOrDefault(x => x.Culture.Name == lang).Name = category.Name;
            }

            _dbContext.Save();

            return "OK";
        }

        //need to improve
        public string SaveProducts(Store_Product_WithProperties product, string lang)
        {

            if (product.Id != 0)
            {
                var existedProduct = _dbContext.Get<Store_Product>(x => x.Id == product.Id).SingleOrDefault();//new Store_Category_WithProperties(_dbContext.Get<Store_Category>(x => x.Id == category.Id).SingleOrDefault(), lang);

                existedProduct.ArtNo = product.ArtNo;
                existedProduct.Discount = product.Discount;
                existedProduct.ItemsSold = product.ItemsSold;
                existedProduct.IsFreeShiping = product.IsFreeShiping;
                existedProduct.IsNew = product.IsNew;
                existedProduct.IsDownloadable = product.IsDownloadable;
                existedProduct.IsOnSale = product.IsOnSale;
                existedProduct.FriendlyUrl = product.FriendlyUrl;
                existedProduct.SortOrder = product.SortOrder;

                existedProduct.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == lang).Name = product.Name;
                existedProduct.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == lang).BriefDescription = product.BriefDescription;
                existedProduct.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == lang).Description = product.Description;
                existedProduct.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == lang).Cost = product.Cost;

                foreach (var property in product.CustomProperties)
                {
                    existedProduct.Store_PropertyValue2Product.SingleOrDefault(x => x.Store_PropertyValue.Store_Property.Name.Equals(property.First, StringComparison.InvariantCultureIgnoreCase)).Store_PropertyValue.Value = property.Second;
                }
            }

            _dbContext.Save();

            return "OK";
        }

        #region Private methods


        #endregion
    }
}
